package com.artron.mediaartron.ui.fragment.made.picture;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.artron.baselib.base.BaseLoadingFragment;
import com.artron.baselib.entity.Response;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.config.AppProfile;
import com.artron.mediaartron.data.entity.MaterialData;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.ui.adapter.PictureSelectionAdapter;
import com.artron.mediaartron.ui.helper.SpacesItemDecoration;
import com.artron.mediaartron.ui.widget.MediaImageLayout;
import com.bilibili.boxing.model.entity.BaseMedia;
import com.bilibili.boxing.model.entity.impl.ImageMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observable;

import static com.umeng.analytics.pro.x.S;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-09-28.
 */

public class PictureNetFragment extends BaseLoadingFragment<MaterialData> implements MediaSelectCallback {

    @BindView(R.id.General_recycler)
    protected RecyclerView mRecyclerView;

    private PictureSelectionAdapter mMediaAdapter;
    private List<MaterialData.MaterialListBean> mData;

    public static PictureNetFragment newInstance() {
        Bundle args = new Bundle();
        PictureNetFragment fragment = new PictureNetFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.general_recycler;
    }


    @Override
    public void initViewWithData(MaterialData data) {
        mData = data.getMaterialList();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 3);
        gridLayoutManager.setSmoothScrollbarEnabled(true);
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(getResources()
                .getDimensionPixelOffset(R.dimen.boxing_media_margin), 3));

        mMediaAdapter = new PictureSelectionAdapter(getActivity());
        mMediaAdapter.setOnCheckedListener(new OnMediaCheckedListener());
        mMediaAdapter.setOnMediaClickListener(new OnMediaClickListener());
        mMediaAdapter.setNeedCamera(false);
        mRecyclerView.setAdapter(mMediaAdapter);


        List<BaseMedia> list = new ArrayList<>();
        for (MaterialData.MaterialListBean bean : data.getMaterialList()) {
            ImageMedia media = new ImageMedia.Builder(bean.getId(),
                    bean.getSmallThumbnailImagePath()).build();
            list.add(media);
        }
        mMediaAdapter.addAllData(list);
    }



    @Override
    protected Observable<Response<MaterialData>> retrofitData() {
        String passId = AppProfile.getUserInfo().getPassId();
        return RetrofitHelper.getMaterialApi().queryMaterial(passId, 1, 100);
    }

    private class OnMediaClickListener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            ImageMedia imageMedia = (ImageMedia) view.getTag();
            MediaImageLayout imageLayout = ((MediaImageLayout) view);
            setCheckView(imageMedia, imageLayout);
        }
    }

    private void setCheckView(ImageMedia imageMedia, MediaImageLayout itemLayout) {
        boolean isSelected = !imageMedia.isSelected();

        List<BaseMedia> allMedias = mMediaAdapter.getAllMedias();
        for (int x = 0; x < allMedias.size(); x++) {
            ImageMedia media = (ImageMedia) allMedias.get(x);
            if (isSelected) {
                media.setSelected(false);
            }
        }

        List<BaseMedia> selectedMedias = mMediaAdapter.getSelectedMedias();
        selectedMedias.clear();
        selectedMedias.add(imageMedia);

        imageMedia.setSelected(isSelected);
        itemLayout.setChecked(isSelected);
        mMediaAdapter.notifyDataSetChanged();

        ((TabFragment) getParentFragment()).setCheckView(isSelected);
    }

    private class OnMediaCheckedListener implements PictureSelectionAdapter.OnMediaCheckedListener {

        @Override
        public void onChecked(View view, BaseMedia iMedia) {
            if (!(iMedia instanceof ImageMedia)) {
                return;
            }

            setCheckView(((ImageMedia) iMedia), ((MediaImageLayout) view));
        }
    }

    public List<? extends BaseMedia> getSelectedMedias() {
        List<ScaleMedia> result = new ArrayList<>();
        List<BaseMedia> selectedMedias = mMediaAdapter.getSelectedMedias();
        for (MaterialData.MaterialListBean bean : mData) {
            for (BaseMedia selectedMedia : selectedMedias) {
                if (selectedMedia.getPath().equals(bean.getSmallThumbnailImagePath())) {
                    ScaleMedia media = new ScaleMedia(bean.getId(), bean.getOriginalImagePath());
                    media.setScale(bean.getScale());
                    long size = 1000 * 1024;
                    media.setSize(size + "");
                    result.add(media);
                }
            }
        }
        return result;
    }

    @Override
    protected int getEmptyViewId() {
        return R.layout.fragment_picture_net;
    }
}
